#!/bin/bash

# 设置严格模式
set -euo pipefail

# 检查 OHOS_SDK_HOME 环境变量
if [ -z "$OHOS_SDK_HOME" ]; then
    echo "错误: OHOS_SDK_HOME 环境变量未设置"
    exit 1
fi

# 颜色输出bu s
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${BLUE}=== 开始构建 OHOS 原生库 ===${NC}"

# 1. 进入 cpp 目录
echo -e "${GREEN}步骤 1: 进入 cpp 目录${NC}"
cd shared/src/nativeInterop/cinterop/cpp || exit 1

# 2. 清理 CMake 相关文件
echo -e "${RED}步骤 2: 清理 CMake 相关文件${NC}"
rm -rf CMakeCache.txt CMakeFiles cmake_install.cmake Makefile build *.a *.so
echo "CMake 相关文件已清理"

# 3. 运行 CMake 配置
echo -e "${GREEN}步骤 3: 运行 CMake 配置${NC}"
"$OHOS_SDK_HOME/native/build-tools/cmake/bin/cmake" \
  -DOHOS_STL=c++_shared \
  -DCMAKE_BUILD_TYPE=Release \
  -DOHOS_PLATFORM=OHOS \
  -DCMAKE_TOOLCHAIN_FILE="$OHOS_SDK_HOME/native/build/cmake/ohos.toolchain.cmake" \
  . && wait

if [ $? -ne 0 ]; then
    echo -e "${RED}CMake 配置失败${NC}"
    exit 1
fi
echo -e "${YELLOW}CMake 配置完成${NC}"

# 4. 构建项目 - 确保同步执行
echo -e "${GREEN}步骤 4: 构建 CMake 项目${NC}"
"$OHOS_SDK_HOME/native/build-tools/cmake/bin/cmake" --build . --verbose 2>&1 | tee build.log
BUILD_STATUS=${PIPESTATUS[0]}

if [ $BUILD_STATUS -ne 0 ]; then
    echo -e "${RED}CMake 构建失败，退出码: $BUILD_STATUS${NC}"
    exit 1
fi

# 等待所有后台进程完成
wait

# 验证生成的文件是否存在
echo -e "${YELLOW}验证构建产物...${NC}"
if [ -f "libresource_utils.a" ]; then
    echo -e "${GREEN}✓ libresource_utils.a 已生成${NC}"
else
    echo -e "${RED}✗ libresource_utils.a 未找到！${NC}"
    exit 1
fi

echo -e "${YELLOW}CMake 构建完成并验证通过${NC}"

# 5. 返回项目根目录
echo -e "${GREEN}步骤 5: 返回项目根目录${NC}"
cd ../../../../.. || exit 1
echo "当前目录: $(pwd)"

# 6. 更新 Compose 项目静态库
echo -e "${GREEN}步骤 6: 更新 Compose 项目静态库${NC}"

RESOURCE_LIB_SRC="shared/src/nativeInterop/cinterop/cpp/libresource_utils.a"
RESOURCE_LIB_DEST="composeApp/src/ohosArm64Main/cinterop/native/libresource_utils.a"

if [ ! -f "$RESOURCE_LIB_SRC" ]; then
    echo -e "${RED}✗ 未找到源文件: $RESOURCE_LIB_SRC${NC}"
    exit 1
fi

DEST_PARENT_DIR=$(dirname "$RESOURCE_LIB_DEST")
mkdir -p "$DEST_PARENT_DIR"

if [ -f "$RESOURCE_LIB_DEST" ]; then
    echo -e "${YELLOW}删除旧的静态库: $RESOURCE_LIB_DEST${NC}"
    rm -f "$RESOURCE_LIB_DEST"
fi

echo -e "${YELLOW}复制 $RESOURCE_LIB_SRC -> $RESOURCE_LIB_DEST${NC}"
cp "$RESOURCE_LIB_SRC" "$RESOURCE_LIB_DEST"
echo -e "${GREEN}✓ 静态库已更新${NC}"

echo -e "${BLUE}=== 发布 Harmony 包 ===${NC}"
./gradlew :composeApp:publishReleaseBinariesToHarmonyApp
GRADLE_EXIT_CODE=$?
if [ $GRADLE_EXIT_CODE -ne 0 ]; then
    echo -e "${RED}✗ Gradle 任务失败，退出码 $GRADLE_EXIT_CODE${NC}"
    exit $GRADLE_EXIT_CODE
fi
echo -e "${GREEN}✓ Harmony 包发布完成${NC}"


## 6. 运行 Gradle clean
#echo -e "${RED}步骤 6: 运行 Gradle clean${NC}"
#./gradlew clean
#if [ $? -ne 0 ]; then
#    echo -e "${RED}Gradle clean 失败${NC}"
#    exit 1
#fi
#wait
#echo -e "${YELLOW}Gradle clean 完成${NC}"
#
## 7. 运行 cinterop
#echo -e "${GREEN}步骤 7: 运行 cinterop${NC}"
#./gradlew :shared:cinteropOhosUtilsOhosArm64 --no-daemon
#if [ $? -ne 0 ]; then
#    echo -e "${RED}cinterop 失败${NC}"
#    exit 1
#fi
#wait
#echo -e "${YELLOW}cinterop 完成${NC}"
#
## 8. 链接共享库
#echo -e "${GREEN}步骤 8: 链接 Release 共享库${NC}"
#./gradlew :shared:linkReleaseSharedOhosArm64 --no-daemon
#if [ $? -ne 0 ]; then
#    echo -e "${RED}链接共享库失败${NC}"
#    exit 1
#fi
#wait
#echo -e "${YELLOW}链接共享库完成${NC}"
#
## 9. 复制生成的文件到目标目录
#echo -e "${GREEN}步骤 9: 复制生成的文件${NC}"
#
## 定义源文件路径
#SO_SOURCE="./shared/build/bin/ohosArm64/releaseShared/libmytest_ohos.so"
#HEADER_SOURCE="./shared/build/bin/ohosArm64/releaseShared/libmytest_ohos_api.h"
#
## 定义目标路径
#SO_DEST="./heif/entry/libs/arm64-v8a/libmytest_ohos.so"
#HEADER_DEST="./heif/entry/src/main/cpp/libmytest_ohos_api.h"
#
## 验证源文件是否存在
#if [ ! -f "$SO_SOURCE" ]; then
#    echo -e "${RED}✗ 源文件不存在: $SO_SOURCE${NC}"
#    exit 1
#fi
#
#if [ ! -f "$HEADER_SOURCE" ]; then
#    echo -e "${RED}✗ 源文件不存在: $HEADER_SOURCE${NC}"
#    exit 1
#fi
#
## 创建目标目录（如果不存在）
#mkdir -p "./heif/entry/libs/arm64-v8a"
#mkdir -p "./heif/entry/src/main/cpp"
#
## 删除旧的 .so 文件
#if [ -f "$SO_DEST" ]; then
#    echo -e "${YELLOW}删除旧的 .so 文件: $SO_DEST${NC}"
#    rm -f "$SO_DEST"
#fi
#
## 删除旧的 .h 文件
#if [ -f "$HEADER_DEST" ]; then
#    echo -e "${YELLOW}删除旧的 .h 文件: $HEADER_DEST${NC}"
#    rm -f "$HEADER_DEST"
#fi
#
## 复制 .so 文件
#echo -e "${YELLOW}复制 $SO_SOURCE -> $SO_DEST${NC}"
#cp "$SO_SOURCE" "$SO_DEST"
#if [ $? -ne 0 ]; then
#    echo -e "${RED}✗ 复制 .so 文件失败${NC}"
#    exit 1
#fi
#echo -e "${GREEN}✓ .so 文件复制成功${NC}"
#
## 复制 .h 文件
#echo -e "${YELLOW}复制 $HEADER_SOURCE -> $HEADER_DEST${NC}"
#cp "$HEADER_SOURCE" "$HEADER_DEST"
#if [ $? -ne 0 ]; then
#    echo -e "${RED}✗ 复制 .h 文件失败${NC}"
#    exit 1
#fi
#echo -e "${GREEN}✓ .h 文件复制成功${NC}"
#
## 验证复制结果
#echo -e "${YELLOW}验证复制的文件...${NC}"
#if [ -f "$SO_DEST" ]; then
#    echo -e "${GREEN}✓ $SO_DEST 存在${NC}"
#    ls -lh "$SO_DEST"
#else
#    echo -e "${RED}✗ $SO_DEST 不存在${NC}"
#    exit 1
#fi
#
#if [ -f "$HEADER_DEST" ]; then
#    echo -e "${GREEN}✓ $HEADER_DEST 存在${NC}"
#    ls -lh "$HEADER_DEST"
#else
#    echo -e "${RED}✗ $HEADER_DEST 不存在${NC}"
#    exit 1
#fi
#
#echo -e "${BLUE}=== 构建和复制完成 ===${NC}"
#echo -e "${GREEN}所有文件已成功复制到目标位置！${NC}"
